package com.googlecode.connectlet.machine.provider.cipher;

import java.security.Key;

import javax.crypto.Cipher;

public final class MachineCbcCipher extends MachineCipher {
	private static final int BLOCK_SIZE = 32768;

	private boolean bEncrypt;
	private byte[] encodedKey;

	public MachineCbcCipher() {
		block = new byte[BLOCK_SIZE];
	}

	@Override
	protected void init(int opmode, Key key) {
		bEncrypt = (opmode == Cipher.ENCRYPT_MODE);
		encodedKey = key.getEncoded();
	}

	@Override
	protected byte[] doFinal(byte[] data) {
		if (bEncrypt) {
			return machineKey.encryptCbc(encodedKey, data);
		}
		return machineKey.decryptCbc(encodedKey, data);
	}
}